﻿// (c) Copyright 2013 Mike Riedel.
// This source is subject to the Microsoft Public License (Ms-PL).
// All other rights reserved.

using System.Runtime.Serialization;
using FreshMeat.Storage;
#if NETFX_CORE || WINDOWS_PHONE
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif

namespace Storage.More.Test
{
   [TestClass]
   public class MsDataContractJsonSerializerTest
   {
      [DataContract]
      public class MyData
      {
         [DataMember]
         public bool Bool { get; set; }
         [DataMember(EmitDefaultValue = false)]
         public string String { get; set; }
         [DataMember]
         public int Int { get; set; }
         [DataMember(EmitDefaultValue = false)]
         public MyData Nested { get; set; }
      }

      [TestMethod]
      public void SerializeDefaultValues()
      {
         var serializer = new MsDataContractJsonSerializer<MyData>();
         var data = new MyData();

         var actual = serializer.Serialize(data);

         const string expected = "{\"Bool\":false,\"Int\":0}";
         Assert.AreEqual(expected, actual);
      }

      [TestMethod]
      public void DeserializeDefaultValues()
      {
         var serializer = new MsDataContractJsonSerializer<MyData>();

         const string valid = "{\"Bool\":false,\"Int\":0}";
         var result = serializer.Deserialize(valid);

         Assert.IsNotNull(result, "Result is null.");
         Assert.AreEqual(false, result.Bool, "Bool is wrong.");
         Assert.AreEqual(null, result.String, "String is wrong.");
         Assert.AreEqual(0, result.Int, "Int is wrong.");
         Assert.AreEqual(null, result.Nested, "Nested is wrong.");
      }

      [TestMethod]
      public void SerializeNonDefaultValues()
      {
         var serializer = new MsDataContractJsonSerializer<MyData>();
         var data = new MyData {Bool = true, Int = 123, Nested = new MyData(), String = "abc"};

         var actual = serializer.Serialize(data);

         const string expected = "{\"Bool\":true,\"Int\":123,\"Nested\":{\"Bool\":false,\"Int\":0},\"String\":\"abc\"}";
         Assert.AreEqual(expected, actual);
      }

      [TestMethod]
      public void DeserializeNonDefaultValues()
      {
         var serializer = new MsDataContractJsonSerializer<MyData>();

         const string valid = "{\"Bool\":true,\"String\":\"abc\",\"Int\":123,\"Nested\":{\"Bool\":false,\"Int\":0}}";
         var result = serializer.Deserialize(valid);

         Assert.IsNotNull(result, "Result is null.");
         Assert.AreEqual(true, result.Bool, "Bool is wrong.");
         Assert.AreEqual("abc", result.String, "String is wrong.");
         Assert.AreEqual(123, result.Int, "Int is wrong.");
         Assert.IsNotNull(result.Nested, "Nested is wrong.");
         Assert.AreEqual(false, result.Nested.Bool, "Nested Bool is wrong.");
         Assert.AreEqual(0, result.Nested.Int, "Nested Int is wrong.");
      }
   }
}